<template>
  <div class="app-container">
    <div class="filter-container">
      <el-page-header @back="backUp" class="backUp" :content="attr_name"></el-page-header>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        添加属性类型
      </el-button>
    </div>
    <el-table
      :data="data"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="value_name" :label="'属性值('+label[labelType-1]+'级)'"  align='center'/>
      <el-table-column label="上级" align='center'>
        <template>
          {{attr_name}}
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align='center'>
        <template slot-scope="item">
          <el-switch
            v-model="item.row.is_show"
            :active-value="1"
            :inactive-value="0"
            @change="attrUpdate(item.row,'is_show')"
          />
        </template>
      </el-table-column>
      <el-table-column label="排序" align='center'>
        <template slot-scope="item">
          <el-input v-model="item.row.sort" type='number' @blur="attrUpdate(item.row,'sort')"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width='280' align='center'>
        <template slot-scope="item">
          <el-link type="primary" class="table_btn" @click="handleDetails(item.row)" :underline="false" v-if="labelType<4"><i class="iconfont icon-chakan"></i>查看</el-link>
          <el-link type="primary" class="table_btn" @click="handleUpdate(item.row)" :underline="false"><i class="iconfont icon-bianji"></i>编辑</el-link>
          <el-link type="primary" class="table_btn" @click="handleCreateChild(item.row)" :underline="false"><i class="el-icon el-icon-plus"></i>添加</el-link>
          <el-link type="danger" class="table_btn" @click="handleDelete(item.row.value_id)" :underline="false"><i class="iconfont icon-lajitong1"></i>删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="pagination-container"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paginate.current"
      :hide-on-single-page='isHideSingle'
      :page-sizes="paginate.sizes"
      :page-size="paginate.limit"
      :layout="paginate.layout"
      :total="paginate.total"/>
      <addChildAttr ref='addChildAttr' @addSuccess='addSuccess'></addChildAttr>
  </div>
</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import addChildAttr from '../components/addChildAttr.vue'
export default {
  name: 'Index',
  mixins: [formOperate],
  data() {
    return {
      url:'/profession/admin/attrValue',
      pk:'value_id',
      attr_name:'',// 类型名称
      queryParam:{
        attr_type:'',
        parent_id:''
      },
      defaultQueryParam:['attr_type','parent_id'],
      labelType:'', // 当前子属型级数
      label:['一','二','三','四'], // 当前子属型级数
    }
  },
  mounted() {
    console.log(this.$route,12)
    this.labelType = this.$route.query.labelType
    this.queryParam.parent_id = this.$route.query.parent_id
    this.queryParam.attr_type = this.$route.query.attr_type
    this.attr_name = this.$route.query.attr_name
    this.$route.meta.title = this.attr_name
  },
  methods: {
    // 添加属性值
    handleCreate() {
      let param = {
        parent_id:this.queryParam.parent_id,
        attr_id:this.queryParam.attr_type,
        attr_name:this.attr_name
      }
      this.$refs.addChildAttr.show(param)
    },
    // 编辑属性值
    handleUpdate(item) {
      let param = {
        parent_id:this.queryParam.parent_id,
        attr_id:this.queryParam.attr_type,
        attr_name:this.attr_name,
        id:item.value_id,
        is_show:item.is_show,
        sort:item.sort,
        value_name:item.value_name
      }
      this.$refs.addChildAttr.show(param,'edit')
    },
    //添加，编辑属性后的回调
    addSuccess() {
      this.getList()
    },
    // 修改属性值是否显示，排序
    attrUpdate(item,type) {
      this.id = item.value_id
      let param = {}
      param[type] = item[type]
      this.$http.put(this.url + '/' + this.id, param).then((response) => {
        this.handleResponse(response)
      })
    },
    // 添加子属性
    handleCreateChild(item) {
      this.$refs.addChildAttr.show(item)
    },
    // 查看子属性列表
    handleDetails(item) {
      let id = ++this.labelType
      this.$router.push({path:'/talents/childAttr/'+id,query:{labelType:id,attr_type:item.attr_type,parent_id:item.value_id,attr_name:item.value_name}})
    },
    backUp() {
      if (this.labelType > 1) {
        this.$router.go(-1)
      } else {
        this.$router.push({path:'/talents/attrs'})
      }
    }
  },
  components:{
    addChildAttr
  }
}
</script>
<style lang="scss" scoped>
.filter-container{
  display: flex;
  align-items: center;
}
.backUp{
  flex:1;
  min-width: 0;
  text-align: left;
}
.el-icon-arrow-left{
  font-size: 16px;
  color: #222;
}
</style>